package net.ib.mtalk.database.helper;

import net.ib.mntalk.protocol.Common.UUIDMsg;
import net.ib.mntalk.protocol.client.Database.FileTransferInfo;
import net.ib.mtalk.database.DatabaseContract.ProfileTable;
import net.ib.mtalk.util.GpbSupport;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.google.protobuf.micro.InvalidProtocolBufferMicroException;

public class ProfileDatabaseHelper {
	public static void setFileTransferInfo(Context context, UUIDMsg profileUuid, FileTransferInfo transferInfo, String mode)
	{
		String where = ProfileTable.KEY_PROFILE_UUID_MSB + "=? AND " + ProfileTable.KEY_PROFILE_UUID_LSB + "=? AND " + ProfileTable.KEY_IS_UPLOAD + "=?";
		String[] whereSelect = {String.valueOf(GpbSupport.msbOf(profileUuid)), String.valueOf(GpbSupport.lsbOf(profileUuid)), mode};
 		Cursor c = context.getContentResolver().query(ProfileTable.CONTENT_URI, null, where, whereSelect, null);
 		if( c == null )	//DB 자체가 없다는 것
 		{
 			return;	
 		}
 		else if(c.getCount() > 0)
 		{
 			ContentValues cv = new ContentValues();
 			cv.put(ProfileTable.KEY_FILE_TRANSFER_INFO, transferInfo.toByteArray());
 			context.getContentResolver().update(ProfileTable.CONTENT_URI, cv, where, whereSelect);
 		}
 		else
 		{
 			ContentValues cv = new ContentValues();
 			cv.put(ProfileTable.KEY_PROFILE_UUID_MSB, String.valueOf(GpbSupport.msbOf(profileUuid)));
 			cv.put(ProfileTable.KEY_PROFILE_UUID_LSB, String.valueOf(GpbSupport.lsbOf(profileUuid)));
 			cv.put(ProfileTable.KEY_FILE_TRANSFER_INFO, transferInfo.toByteArray());
 			cv.put(ProfileTable.KEY_IS_UPLOAD, mode);
 			context.getContentResolver().insert(ProfileTable.CONTENT_URI, cv);
 		}
 		
 		c.close();
	}
	
	public static FileTransferInfo getFileTransferInfo(Context context, UUIDMsg profileUuid, String mode)
	{
		String where = ProfileTable.KEY_PROFILE_UUID_MSB + "=? AND " + ProfileTable.KEY_PROFILE_UUID_LSB + "=? AND " + ProfileTable.KEY_IS_UPLOAD + "=?";
		String[] whereSelect = {String.valueOf(GpbSupport.msbOf(profileUuid)), String.valueOf(GpbSupport.lsbOf(profileUuid)), mode};
 		Cursor c = context.getContentResolver().query(ProfileTable.CONTENT_URI, null, where, whereSelect, null);
 		
 		if( c == null )	//DB 자체가 없다는 것
 		{
 			return null;	
 		}
 		
 		try {
			return FileTransferInfo.parseFrom(c.getBlob(c.getColumnIndexOrThrow(ProfileTable.KEY_FILE_TRANSFER_INFO)));
		} catch (InvalidProtocolBufferMicroException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}finally{
			c.close();
		}
		
		return null;
	}
	
	public static void removeTransferInfo(Context context, UUIDMsg profileUuid, String mode)
	{
		String where = ProfileTable.KEY_PROFILE_UUID_MSB + "=? AND " + ProfileTable.KEY_PROFILE_UUID_LSB + "=? AND " + ProfileTable.KEY_IS_UPLOAD + "=?";
		String[] whereSelect = {String.valueOf(GpbSupport.msbOf(profileUuid)), String.valueOf(GpbSupport.lsbOf(profileUuid)), mode};
 		context.getContentResolver().delete(ProfileTable.CONTENT_URI, where, whereSelect);
	}
}
